
import {LGXEnums} from '../../common/sysenum.js'
var dirModeEnums = LGXEnums.LGXLineGradientDirMode;
import { CoordTRFUtil } from '../../common/tools/coordtool.js'
import lgxcom from '../../common/common.js'
import utilfuncs from '../../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;


var LineGradientUtil = {}

LineGradientUtil.getGradientCoord = function (dirMode, graph, mapinfo,isPaintSybolUnitFlag) {
    let poslist = this.getGradientCoord_World(dirMode, graph,isPaintSybolUnitFlag);
    let screenPtLst = null;
    if (poslist) {
        screenPtLst = [];
        if (!isPaintSybolUnitFlag){
            for (let i = 0; i < poslist.length; i++) {
                let pt = poslist[i];
                var screenPt = CoordTRFUtil.world2Screen(pt, mapinfo);
                screenPtLst.push(screenPt);
            }
        }
        else{
            let scaling = mapinfo.getScaling();
            for (let i = 0; i < poslist.length; i++) {
                let pt = poslist[i];
                let x = pt.x*scaling;
                let y = -pt.y*scaling;
                let screenPt = {x,y};
                screenPtLst.push(screenPt);
            }
        }
    }
    return screenPtLst;
}


LineGradientUtil.getGradientCoord_World = function (dirMode, graph,isPaintSybolUnitFlag) {
    let poslist = null;
    let localRect = graph.getLocalZone();
    if (isPaintSybolUnitFlag)
    {
        localRect = graph.getZone();
    }
    switch (dirMode) {
        case dirModeEnums.L_TO_R:			//左-右
            poslist = this.left2right(localRect);
            break;

        case dirModeEnums.R_TO_L:				//右-左
            poslist = this.right2left(localRect);
            break;

        case dirModeEnums.T_TO_B:				//上-下
            poslist = this.top2bottom(localRect);
            break;

        case dirModeEnums.B_TO_T:				//下-上
            poslist = this.bottom2top(localRect);
            break;

        case dirModeEnums.LT_TO_RB:				//左上-右下
            poslist = this.leftTop2rightBottom(localRect);
            break;

        case dirModeEnums.LB_TO_RT:				//左下-右上
            poslist = this.leftBottom2rightTop(localRect);
            break;

        case dirModeEnums.RT_TO_LB:				//右上-左下
            poslist = this.rightTop2leftBottom(localRect);
            break;

        case dirModeEnums.RB_TO_LT:				//右下-左上
            poslist = this.rightBottom2leftTop(localRect);
            break;
    }
    let mtx = graph.getLocalMatrix();
    for (let i = 0; i < poslist.length; i++) {
        let pt = poslist[i];
        let worldPt = mtx.MultT(pt);//求相对于控制图形局部坐标系的局部坐标  
        pt.x = worldPt.x;
        pt.y = worldPt.y;
    }
    
    return poslist;
}

LineGradientUtil.getGradientCoord_bak = function (dirMode, graph, mapinfo,isPaintSybolUnitFlag) {
    let poslist = null;
    let mtx = graph.getLocalMatrix();
    let localRect = graph.getLocalZone();
    if (isPaintSybolUnitFlag){
        localRect = graph.getZone();
    }
    switch (dirMode) {
        case dirModeEnums.L_TO_R:			//左-右
            poslist = this.left2right(localRect);
            break;

        case dirModeEnums.R_TO_L:				//右-左
            poslist = this.right2left(localRect);
            break;

        case dirModeEnums.T_TO_B:				//上-下
            poslist = this.top2bottom(localRect);
            break;

        case dirModeEnums.B_TO_T:				//下-上
            poslist = this.bottom2top(localRect);
            break;

        case dirModeEnums.LT_TO_RB:				//左上-右下
            poslist = this.leftTop2rightBottom(localRect);
            break;

        case dirModeEnums.LB_TO_RT:				//左下-右上
            poslist = this.leftBottom2rightTop(localRect);
            break;

        case dirModeEnums.RT_TO_LB:				//右上-左下
            poslist = this.rightTop2leftBottom(localRect);
            break;

        case dirModeEnums.RB_TO_LT:				//右下-左上
            poslist = this.rightBottom2leftTop(localRect);
            break;
    }

    let screenPtLst = null;
    if (poslist) {
        screenPtLst = [];
        if (!isPaintSybolUnitFlag){
            for (let i = 0; i < poslist.length; i++) {
                let pt = poslist[i];
                let worldPt = mtx.MultT(pt);//求相对于控制图形局部坐标系的局部坐标  
                var screenPt = CoordTRFUtil.world2Screen(worldPt, mapinfo);
                screenPtLst.push(screenPt);
            }
        }
        else{
            let scaling = mapinfo.getScaling();
            for (let i = 0; i < poslist.length; i++) {
                let pt = poslist[i];
                let x = pt.x*scaling;
                let y = -pt.y*scaling;
                let screenPt = {x,y};
                screenPtLst.push(screenPt);
            }
        }
        
    }
    return screenPtLst;
}

LineGradientUtil.left2right = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };
    let p2 = { x: localRect.right, y: localRect.top, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.right2left = function (localRect) {
    let p1 = { x: localRect.right, y: localRect.top, z: 0 };
    let p2 = { x: localRect.left, y: localRect.top, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.top2bottom = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };
    let p2 = { x: localRect.left, y: localRect.bottom, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.bottom2top = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.bottom, z: 0 };
    let p2 = { x: localRect.left, y: localRect.top, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.leftTop2rightBottom = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };
    let p2 = { x: localRect.right, y: localRect.bottom, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.leftBottom2rightTop = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.bottom, z: 0 };
    let p2 = { x: localRect.right, y: localRect.top, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.rightTop2leftBottom = function (localRect) {
    let p1 = { x: localRect.right, y: localRect.top, z: 0 };
    let p2 = { x: localRect.left, y: localRect.bottom, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}

LineGradientUtil.rightBottom2leftTop = function (localRect) {
    let p1 = { x: localRect.right, y: localRect.bottom, z: 0 };
    let p2 = { x: localRect.left, y: localRect.top, z: 0 };
    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(p2);
    return ptlst;
}


LineGradientUtil.left_Mid_Right = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };
    let midx = (localRect.left + localRect.right) / 2;
    let midpt = { x: midx, y: localRect.top, z: 0 };
    let p2 = { x: localRect.right, y: localRect.top, z: 0 };

    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(midpt);
    ptlst.push(p2);
    return ptlst;
}


LineGradientUtil.right_Mid_left = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };

    let midx = (localRect.left + localRect.right) / 2;
    let midpt = { x: midx, y: localRect.top, z: 0 };

    let p2 = { x: localRect.right, y: localRect.top, z: 0 };

    let ptlst = [];
    ptlst.push(p2);
    ptlst.push(midpt);
    ptlst.push(p1);
    return ptlst;
}

LineGradientUtil.top_Mid_Bottom = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };

    let midy = (localRect.top + localRect.bottom) / 2;
    let midpt = { x: localRect.left, y: midy, z: 0 };

    let p2 = { x: localRect.left, y: localRect.bottom, z: 0 };

    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(midpt);
    ptlst.push(p2);
    return ptlst;
}


LineGradientUtil.bottom_Mid_top = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };

    let midy = (localRect.top + localRect.bottom) / 2;
    let midpt = { x: localRect.left, y: midy, z: 0 };

    let p2 = { x: localRect.left, y: localRect.bottom, z: 0 };

    let ptlst = [];
    ptlst.push(p2);
    ptlst.push(midpt);
    ptlst.push(p1);
    return ptlst;
}


LineGradientUtil.leftTop_Mid_RightBottom = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.top, z: 0 };

    let midx = (localRect.left + localRect.right) / 2;
    let midy = (localRect.top + localRect.bottom) / 2;
    let midpt = { x: midx, y: midy, z: 0 };

    let p2 = { x: localRect.right, y: localRect.bottom, z: 0 };

    let leftBotPt = { x: localRect.left, y: localRect.bottom, z: 0 };
    let rightTopPt = { x: localRect.right, y: localRect.top, z: 0 };
    let vector1 = new lgxcom.LGXVector();
    vector1.setBy2Pt(leftBotPt, rightTopPt);

    //正交线
    let vector2 = new lgxcom.LGXVector();
    vector2.setBy2Pt(leftBotPt, rightTopPt);
    vector2.rotate(90);

    let interPt = funcs.getTwoLineInterPos(p2, vector1, p1, vector2);

    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(midpt);
    ptlst.push({ x: interPt.x, y: interPt.y, z: 0 });
    return ptlst;
}

LineGradientUtil.rightBottom_Mid_leftTop = function (localRect) {
    let p1 = { x: localRect.right, y: localRect.bottom, z: 0 };

    let midx = (localRect.left + localRect.right) / 2;
    let midy = (localRect.top + localRect.bottom) / 2;
    let midpt = { x: midx, y: midy, z: 0 };

    let p2 = { x: localRect.left, y: localRect.top, z: 0 };

    //右上
    let p3 = { x: localRect.right, y: localRect.top, z: 0 };

    //左下
    let p4 = { x: localRect.left, y: localRect.bottom, z: 0 };
    let vector1 = new lgxcom.LGXVector();
    vector1.setBy2Pt(p3, p4);

    //正交线
    let vector2 = new lgxcom.LGXVector();
    vector2.setBy2Pt(p3, p4);
    vector2.rotate(90);

    let interPt = funcs.getTwoLineInterPos(p2, vector1, p1, vector2);

    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(midpt);
    ptlst.push({ x: interPt.x, y: interPt.y, z: 0 });
    return ptlst;
}

LineGradientUtil.rightTop_Mid_leftBottom = function (localRect) {

    let p1 = { x: localRect.right, y: localRect.top, z: 0 };

    let midx = (localRect.left + localRect.right) / 2;
    let midy = (localRect.top + localRect.bottom) / 2;
    let midpt = { x: midx, y: midy, z: 0 };

    let p2 = { x: localRect.left, y: localRect.bottom, z: 0 };

    //右下
    let p3 = { x: localRect.right, y: localRect.bottom, z: 0 };

    //左上
    let p4 = { x: localRect.left, y: localRect.top, z: 0 };
    let vector1 = new lgxcom.LGXVector();
    vector1.setBy2Pt(p3, p4);

    //正交线
    let vector2 = new lgxcom.LGXVector();
    vector2.setBy2Pt(p3, p4);
    vector2.rotate(90);

    let interPt = funcs.getTwoLineInterPos(p2, vector1, p1, vector2);

    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(midpt);
    ptlst.push({ x: interPt.x, y: interPt.y, z: 0 });
    return ptlst;
}

LineGradientUtil.leftBottom_Mid_rightTop = function (localRect) {
    let p1 = { x: localRect.left, y: localRect.bottom, z: 0 };

    let midx = (localRect.left + localRect.right) / 2;
    let midy = (localRect.top + localRect.bottom) / 2;
    let midpt = { x: midx, y: midy, z: 0 };

    let p2 = { x: localRect.right, y: localRect.top, z: 0 };

    //左上
    let p3 = { x: localRect.left, y: localRect.top, z: 0 };

    //右下
    let p4 = { x: localRect.right, y: localRect.bottom, z: 0 };
    let vector1 = new lgxcom.LGXVector();
    vector1.setBy2Pt(p3, p4);

    //正交线
    let vector2 = new lgxcom.LGXVector();
    vector2.setBy2Pt(p3, p4);
    vector2.rotate(90);

    let interPt = funcs.getTwoLineInterPos(p2, vector1, p1, vector2);

    let ptlst = [];
    ptlst.push(p1);
    ptlst.push(midpt);
    ptlst.push({ x: interPt.x, y: interPt.y, z: 0 });
    return ptlst;
}

export default { LineGradientUtil }
